package com.pedro.streamer;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.util.Log;
import android.view.Surface;


public class RefFileVirtual {
    private static final String TAG = "RefFileVirtual";
    //虚拟surface，传递给camera相关参数
    public Surface virtualSurface;
    public SurfaceTexture virtualSurfaceTexture;
    //player相关，
    RefFilePlayer player;
    Surface previewSurface;
    private boolean need_recreate;


    private Surface create_virtual_surface() {
        if (need_recreate) {
            if (virtualSurfaceTexture != null) {
                virtualSurfaceTexture.release();
                virtualSurfaceTexture = null;
            }
            if (virtualSurface != null) {
                virtualSurface.release();
                virtualSurface = null;
            }
            virtualSurfaceTexture = new SurfaceTexture(15);
            virtualSurface = new Surface(virtualSurfaceTexture);
            need_recreate = false;
        } else {
            if (virtualSurface == null) {
                need_recreate = true;
                virtualSurface = create_virtual_surface();
            }
        }
        Log.e(TAG, "重建vituralSurface" + virtualSurface.toString());
        return virtualSurface;
    }

    void createRefFilePlayer(Context context) {
        if (player != null) {
            player.stop();
            player.release();
        }
        player = new RefFilePlayer(context);
    }



    void play() {
        if (previewSurface == null) {
            Log.e(TAG, "call play but previewSurface is null");
            return;
        }
        if (player == null) {
            Log.e(TAG, "call play but player is null");
            return;
        }
        player.setSurface(previewSurface);
        player.setDataSource("");
        player.prepare();
        player.stop();
    }


    public Surface getVirtualSurface() {
        return virtualSurface;
    }

    public void setVirtualSurface(Surface virtualSurface) {
        this.virtualSurface = virtualSurface;
    }

    public SurfaceTexture getVirtualSurfaceTexture() {
        return virtualSurfaceTexture;
    }

    public void setVirtualSurfaceTexture(SurfaceTexture virtualSurfaceTexture) {
        this.virtualSurfaceTexture = virtualSurfaceTexture;
    }

    public RefFilePlayer getPlayer() {
        return player;
    }

    public void setPlayer(RefFilePlayer player) {
        this.player = player;
    }

    public Surface getPreviewSurface() {
        return previewSurface;
    }

    public void setPreviewSurface(Surface previewSurface) {
        this.previewSurface = previewSurface;
    }

    public boolean isNeed_recreate() {
        return need_recreate;
    }

    public void setNeed_recreate(boolean need_recreate) {
        this.need_recreate = need_recreate;
    }

}
